/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.TrainTslj;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.service.TrainTsljService;
import me.zhengjie.domain.vo.TrainTsljQueryCriteria;
import me.zhengjie.mapper.TrainTsljMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author huangwencong
* @date 2024-10-26
**/
@Service
@RequiredArgsConstructor
public class TrainTsljServiceImpl extends ServiceImpl<TrainTsljMapper, TrainTslj> implements TrainTsljService {

    private final TrainTsljMapper trainTsljMapper;

    @Override
    public TrainTslj findOne(Long trainId, Long trainItemId){
        return trainTsljMapper.findOne(trainId, trainItemId);
    }

    @Override
    public PageResult<TrainTslj> queryAll(TrainTsljQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(trainTsljMapper.findAll(criteria, page));
    }

    @Override
    public List<TrainTslj> queryAll(TrainTsljQueryCriteria criteria){
        return trainTsljMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TrainTslj resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TrainTslj resources) {
        TrainTslj trainTslj = getById(resources.getId());
        trainTslj.copy(resources);
        saveOrUpdate(trainTslj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TrainTslj> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TrainTslj trainTslj : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("训练ID", trainTslj.getTrainId());
            map.put("训练项目ID", trainTslj.getTrainItemId());
            map.put("弹射高度(0:30cm,1:70:cm,2:110cm,3:150cm)", trainTslj.getTsgd());
            map.put("特情模拟(0:鸟撞,1:地面故障,2:导弹命中,3:电源故障,4:)", trainTslj.getMntq());
            map.put("弹射动作评定(弹射时机的判断|双腿收回脚蹬紧|臀部后移背靠紧|肌肉紧缩头靠紧|咬牙闭眼臂夹紧|头身两腿要正直)", trainTslj.getTsdz());
            map.put("建议措施(加强时机训练|加强弹射动作要领训练|继续保持|其他)", trainTslj.getJycs());
            map.put("结果判定(0:良好,1:合格,2:不合格)", trainTslj.getResult());
            map.put("记录日期", trainTslj.getDate());
            map.put("记录人", trainTslj.getCreateBy());
            map.put("创建时间", trainTslj.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}